#include <bits/stdc++.h>
using namespace std;
int n,plac[20];
bool check(string s){
	int cnt1=0,cnt2=0,mao;
	for (int i=1;i<=19;i++){
		plac[i]=0;
	}
	for (int i=0;i<s.length();i++){
		if(s[i]=='.'){
			cnt1++;
			plac[cnt1]=i;
		}
		else if(s[i]==':'){
			cnt2++;
			mao=i;
		}
	}
	if(cnt2!=1||cnt1!=3){
		return false;
	}
	if(mao<=plac[3]){
		return false;
	}
	if(s[0]=='0'||s[plac[1]+1]=='0'||s[plac[2]+1]=='0'||s[plac[3]+1]=='0'||mao+1=='0'){
		return false;
	}
	int fir=0,sec=0,thi=0,fou=0,fif=0;
	for (int i=0;i<plac[1];i++){
		fir=fir*10+(s[i]-'0'); 
	}
	for (int i=plac[1]+1;i<plac[2];i++){
		sec=sec*10+(s[i]-'0'); 
	}
	for (int i=plac[2]+1;i<plac[3];i++){
		thi=thi*10+(s[i]-'0'); 
	}
	for (int i=plac[3]+1;i<mao-1;i++){
		fou=fou*10+(s[i]-'0'); 
	}
	for (int i=mao+1;i<s.length();i++){
		fif=fif*10+(s[i]-'0'); 
	}
	if(fir>=0&&fir<=255&&sec>=0&&sec<=255&&thi>=0&&thi<=255&&fou>=0&&fou<=255&&fif>=0&&fif<=65535){
		return true;
	}
	return false;
}
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin >> n;
	string anse[1010];
	string s[1010],c[1010];
	int nums[1010],numc[1010],scnt=0,ccnt=0;
	for (int i=1;i<=n;i++){
		anse[i]="-1";
	}
	for (int i=1;i<=n;i++){
		string op;
		cin >> op;
		string ad;
		cin >> ad;
		if(!check(ad)){
			anse[i]="ERR";
		}
		else{
			if(op=="Server"){
				s[++scnt]=ad;
				nums[scnt]=i;
			}
			else{
				c[++ccnt]=ad;
				numc[ccnt]=i;
			}
		}
	}
	for (int i=1;i<=scnt;i++){
		for (int j=i+1;j<=scnt;j++){
			if(s[j]==s[i]){
				anse[nums[j]]="FAIL";
			}
		}
	}
	for (int i=1;i<=scnt;i++){
		if(anse[nums[i]]!="FAIL"){
			anse[nums[i]]="OK";
		}
	}
	for (int i=1;i<=ccnt;i++){
		for (int j=1;j<=scnt;j++){
			if(s[j]==c[i]&&anse[nums[j]]!="FAIL"){
				anse[numc[i]]=char(nums[j]+'0');
			}
		}
	}
	for (int i=1;i<=n;i++){
		if(anse[i]=="-1"){
			cout << "FAIL" << endl;
			continue;
		}
		cout << anse[i] << endl;
	}
}
/*
5
Server 192.168.1.1:8080
Server 192.168.1.1:8080
Client 192.168.1.1:8080
Client 192.168.1.1:80
Client 192.168.1.1:99999
10
Server 192.168.1.1:80
Client 192.168.1.1:80
Client 192.168.1.1:8080
Server 192.168.1.1:80
Server 192.168.1.1:8080
Server 192.168.1.999:0
Client 192.168.1.1.8080
Client 192.168.1.1:8080
Client 192.168.1.1:80
Client 192.168.1.999:0
*/
